define([ "jquery","glable_mask","mustache.min","comm","text!/static/page/dialog.html"],  function($,Glable_mask,Mustache,comm,dialogTpl) {
	
	//require(['plugin/jquery.ajax.ext'],function(){});
	
	function Dialog(obj, callFun) {
		this.obj = obj;
		this.callFun = callFun;
		this.cancleFun = null;
		this.forceClose = true;
	}


	Dialog.prototype.init = function() {
		var view = {
			title : this.obj.title == null ? "警告" : this.obj.title,
			content : this.obj.content,
			btn_text : this.obj.btn_text == null ? "继续" : this.obj.btn_text
		};
		var that = this;
		var renderedPage = Mustache.to_html(dialogTpl, view);
		if($("#glable_mask").length<=0)
		{
			var glable_mask = new Glable_mask();
			glable_mask.add($(renderedPage));
		}

		$("#dialog p span").html(that.obj.content);
		if(that.obj.open){
			$("#dialog .warn_icon").hide();
			$("#dialog p").css("margin-left","0px");
			$("#dialog").find(".btn-dialog").removeClass("btn-dialog");
			$("#dialog").find(".btn-default").addClass("btn-allow");

		}
		that.change_css();
		$("#dialog").show();
		$("#dialog").find(".close").click(function(e) {
			comm.stopEvent(e);
			/*glable_mask.remove();
			glable_mask = null;*/
			that.close();
			if(that.cancleFun != null){
				that.cancleFun();
			}
		});
		that.bindBtnClick($("#dialog").find(".btn-default"),function(t){
			that.callFun(that.obj.file_id,t);
		});
		
		return false;
	}

	Dialog.prototype.close = function()
	{
		$("#glable_mask").remove();
		$("#dialog_shade").remove();
	}

	Dialog.prototype.bindBtnClick = function(btn,cb,loadingText){
		var that = this;
		loadingText = loadingText ||"操作中...";
		btn.click(function(e) {
			e.stopPropagation();
			var me = $(this);
			me.attr({"disabled":true}).attr("data-value",me.text()).text(loadingText);
			$("body").one("ajaxComplete",function(){
				if(typeof(me)!="undefined")
				{
					me.text(me.attr("data-value")).attr({"disabled":false});
					if(that.forceClose)
					{
						that.close();
					}
				}
			});
			cb(that);
		});
	}
	
	Dialog.prototype.change_css = function() {
		$("#dialog").css("opacity", "0");
		$("#dialog").show();
		var realWidth = parseInt($("#dialog p span").width());
		$("#dialog p").width(realWidth);
		if ($("#dialog .warn_icon").css("display") != "none") {
			realWidth += parseInt($("#dialog .warn_icon").width());
			realWidth += parseInt($("#dialog p").css("margin-left"));
		}

		$("#dialog .content").width(realWidth);
		$("#dialog .content").css("margin", "18px auto 0 auto");
		$("#dialog").css("opacity", "1");
	}
	
	return Dialog;
});